package com.cskaoyan.market.controller.Admin;
/*
* *文件名: AdminKeywordController
 *创建者:wxin
 *创建时间:2024/5/6 21:54*/

import com.cskaoyan.market.db.domain.MarketKeyword;
import com.cskaoyan.market.service.admin.AdminKeywordService;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/admin/keyword")
public class AdminKeywordController {
    @Autowired
    private AdminKeywordService keywordService;
    @PostMapping("delete")
    public Object delete(@RequestBody Map map) throws IOException {
        Integer id = (Integer) map.get("id");
        if (id == null) {
            Object fail = ResponseUtil.fail();
            return fail;
        }
        int result = keywordService.logicalDelete(id);
        if (result == 0) {
            Object fail = ResponseUtil.fail();
            return fail;
        }
        return ResponseUtil.ok();
    }
    @PostMapping("update")
    public Object update(@RequestBody MarketKeyword marketKeyword) throws IOException {
        String keyword = marketKeyword.getKeyword();
        if (StringUtils.isEmpty(keyword)) {
            Object fail = ResponseUtil.fail(404, "关键词不能为空");
            return fail;
        }
        MarketKeyword resultMarketKeyword = keywordService.updataKeyword(marketKeyword);
        HashMap<String, Object> data = new HashMap<>();
        data.put("id", resultMarketKeyword.getId());
        data.put("addTime", resultMarketKeyword.getAddTime());
        data.put("isDefault", resultMarketKeyword.getIsDefault());
        data.put("isHot", resultMarketKeyword.getIsHot());
        data.put("keyword", resultMarketKeyword.getKeyword());
        data.put("updateTime", resultMarketKeyword.getUpdateTime());
        data.put("url", resultMarketKeyword.getUrl());
        return ResponseUtil.ok(data);

    }
    @PostMapping("create")
    public Object create(@RequestBody MarketKeyword marketKeyword) throws IOException {
        String keyword = marketKeyword.getKeyword();
        //String requestBody = JacksonUtil.getObjectMapper().writeValueAsString(map);
        if (StringUtils.isEmpty(keyword)) {
            Object fail = ResponseUtil.fail(404, "关键词不为空");
            return fail;
        }
        MarketKeyword resultMarketKeyword = keywordService.addKeyword(marketKeyword);
        HashMap<String, Object> data = new HashMap<>();
        data.put("id", resultMarketKeyword.getId());
        data.put("addTime", resultMarketKeyword.getAddTime());
        data.put("isDefault", resultMarketKeyword.getIsDefault());
        data.put("isHot", resultMarketKeyword.getIsHot());
        data.put("keyword", resultMarketKeyword.getKeyword());
        data.put("updateTime", resultMarketKeyword.getUpdateTime());
        data.put("url", resultMarketKeyword.getUrl());
        return ResponseUtil.ok(data);
    }
    @GetMapping("list")
    public Object list(String page, String limit,String keyword,String url,String sort,String order) throws IOException {
        Integer pageParam = null;
        Integer limitParam = null;
        try {
            pageParam = Integer.parseInt(page);
            limitParam = Integer.parseInt(limit);

        } catch (NumberFormatException e) {
            Object object = ResponseUtil.badArgument();
            return object;
        }
        List<MarketKeyword> adminKeywordList = keywordService.list(pageParam, limitParam, keyword, url, sort, order);
       return ResponseUtil.okList(adminKeywordList);
    }

}
